package com.mexico.inloancash.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.mingle.widget.LoadingView;
import com.mexico.inloancash.MyApplication;
import com.mexico.inloancash.R;
import com.mexico.inloancash.adapter.IndiaBillAccountAdapter;
import com.mexico.inloancash.bean.India_Billing_ListBean;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.http.IndiaDKFactory;
import com.mexico.inloancash.utils.MyPreferencesManager;
import com.mexico.inloancash.utils.MyToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 账单
 */
public class IndiaBillActivity extends BaseActivity {

    @BindView(R.id.logon_back)
    ImageView mIndiaGonBack;

    @BindView(R.id.title)
    TextView mTitle;

    @BindView(R.id.bill_recycle)
    RecyclerView mRecyclerView;

    @BindView(R.id.refresh)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.empty_linear)
    LinearLayout mIndiaEmpty_Layout;

    @BindView(R.id.loadView)
    LoadingView mLoadingView;

    @BindView(R.id.data_linear)
    LinearLayout mDataLayout;

    private int page = 1;
    private int limit = 10;
    private Handler handler = new Handler();
    private boolean isFresh = false;//判断是否有分页,false有分页数据，true没有分页数据
    private List<India_Billing_ListBean.DataBean> list;
    private IndiaBillAccountAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_my_view_bill_layout);
        ButterKnife.bind(this);
        mTitle.setText("Bill");
        list = new ArrayList<>();
        adapter = new IndiaBillAccountAdapter();
        mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.theme);
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        india_RelaseData();
                    }
                }, 1000);
            }
        });
        mSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        if (!isFresh) {
                            page += 1;
                            getBillAccountInfo(false);
                        } else {
                            //已获取完成所有数据
                            MyToast.show(IndiaBillActivity.this, "All data has been collected");
                        }
                    }
                }
            }
        });
        india_RelaseData();
    }

    //数据还原
    public void india_RelaseData() {
        page = 1;
        getBillAccountInfo(true);
    }

    @OnClick(R.id.logon_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 获取订单信息
     */
    public void getBillAccountInfo(boolean isRefresh) {
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> getBill = api.getBill(MyPreferencesManager.getString("token",""), MyApplication.source);
        getBill.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try {
                    if (response.isSuccessful()) {
                        India_Billing_ListBean indiaBillingListBean = new Gson().fromJson(response.body(), India_Billing_ListBean.class);
                        if (indiaBillingListBean.getCode() == 200) {
                            if (isRefresh) {
                                list.clear();
                            }
                            if (indiaBillingListBean.getData().size() < limit) {
                                isFresh = true;
                            }
                            list.addAll(indiaBillingListBean.getData());
                            adapter.setNewData(list);
                            if (list.size() > 0) {
                                mIndiaEmpty_Layout.setVisibility(View.GONE);
                                mSwipeRefreshLayout.setVisibility(View.VISIBLE);
                            } else {
                                mIndiaEmpty_Layout.setVisibility(View.VISIBLE);
                                mSwipeRefreshLayout.setVisibility(View.GONE);
                            }
                        }else if(indiaBillingListBean.getCode() == 401){
                            Intent intent = new Intent(IndiaBillActivity.this, IndiaLoginActivity.class);
                            startActivity(intent);
                        }
                    }
                }catch (Exception e){

                }
                mSwipeRefreshLayout.setRefreshing(false);
                mLoadingView.setVisibility(View.GONE);
                mDataLayout.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                mSwipeRefreshLayout.setRefreshing(false);
                mLoadingView.setVisibility(View.GONE);
                mDataLayout.setVisibility(View.VISIBLE);
            }
        });
    }
}
